package com.dotstone.chipism.view;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.dotstone.chipism.R;
import com.dotstone.chipism.activity.SceneSettingActivity;
import com.dotstone.chipism.activity.WirelessSwitchActivity;
import com.dotstone.chipism.bean.EventBusEntity;
import com.dotstone.chipism.bean.HttpURL;
import com.dotstone.chipism.bean.Scene;
import com.dotstone.chipism.fragment.MainFragment;
import com.dotstone.chipism.ui.timing.RemoteSocketTimingActivity;
import com.dotstone.chipism.ui.timing.bean.DeleteTimingBean;
import com.dotstone.chipism.util.DeviceManager;
import com.dotstone.chipism.util.SPUtils;
import com.google.gson.Gson;
import com.google.gson.JsonIOException;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class SancePopWindow extends PopupWindow implements OnClickListener {
	private View contentView1;
	private LinearLayout ll_1;
	private LinearLayout ll_2;
	private LinearLayout ll_3;
	private LinearLayout ll_4;
	private Context context;
	private String deviceid;
	private String devicename;
	private String sceneid;
	private int position;
	private int bsid;
	private int h;
	private int w;

	public SancePopWindow(final Activity contextx, View parent, int position,String deviceid,int bsid,String sceneid) {
		this.context = contextx;
		this.deviceid = deviceid;
		this.bsid = bsid;
		this.sceneid = sceneid;
		this.position = position;
		LayoutInflater mLayoutInflater = LayoutInflater.from(context);
		contentView1 = mLayoutInflater
				.inflate(R.layout.scene_popuwindow, null);
		h = contextx.getWindowManager().getDefaultDisplay().getHeight();
		w = contextx.getWindowManager().getDefaultDisplay().getWidth();
		// 设置SelectPicPopupWindow的View
		this.setContentView(contentView1);
		// 设置SelectPicPopupWindow弹出窗体的宽
		this.setWidth(w / 2 + 150);
		// 设置SelectPicPopupWindow弹出窗体的高
		this.setHeight(LayoutParams.WRAP_CONTENT);
		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(true);
		this.setOutsideTouchable(true);
		// 实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(0x00000000);
		// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
		this.setBackgroundDrawable(dw);
		// 产生背景变暗效果
		WindowManager.LayoutParams lp = contextx.getWindow().getAttributes();
		lp.alpha = 0.8f;
		contextx.getWindow().setAttributes(lp);
		// 刷新状态
		this.update();
		// 以下拉方式显示popupwindow
		// popuWindow1.showAsDropDown(parent, 180, 180);
		this.setOnDismissListener(new OnDismissListener() {

			// 在dismiss中恢复透明度
			public void onDismiss() {
				WindowManager.LayoutParams lp = contextx.getWindow()
						.getAttributes();
				lp.alpha = 1f;
				contextx.getWindow().setAttributes(lp);
			}
		});
		ll_1 = (LinearLayout) contentView1.findViewById(R.id.ll_1);
		ll_2 = (LinearLayout) contentView1.findViewById(R.id.ll_2);
		ll_3 = (LinearLayout) contentView1.findViewById(R.id.ll_3);
		ll_4 = (LinearLayout) contentView1.findViewById(R.id.ll_4);
		if(bsid!=-1){
			ll_1.setVisibility(View.VISIBLE);
			ll_2.setVisibility(View.GONE);
			ll_3.setVisibility(View.VISIBLE);
			ll_4.setVisibility(View.VISIBLE);
		}else{
			ll_1.setVisibility(View.GONE);
			ll_2.setVisibility(View.VISIBLE);
			ll_3.setVisibility(View.GONE);
			ll_4.setVisibility(View.GONE);
		}

		ll_1.setOnClickListener(this);
		ll_2.setOnClickListener(this);
		ll_3.setOnClickListener(this);
		ll_4.setOnClickListener(this);

	}

	/**
	 * 显示popupWindow
	 * 
	 * @param parent
	 */
	public void showPopupWindow(View parent) {
		if (!this.isShowing()) {
			// 显示在左上方
			this.showAtLocation(parent, Gravity.TOP, 0,
					(int) (0.42* h));
			// 以下拉方式显示popupwindow
			this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 180);
		} else {
			this.dismiss();
		}
	}

	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
		case R.id.ll_1:
			if(!TextUtils.isEmpty(sceneid)){
				MainFragment.instance.openScene1(sceneid,false);
				MainFragment.instance.openScene2(sceneid);
			}
			SancePopWindow.this.dismiss();
			break;
		case R.id.ll_2:
			intent=new Intent(context,SceneSettingActivity.class);
			intent.putExtra("position",position);
			intent.putExtra("deviceid",deviceid);
			intent.putExtra("sceneid",sceneid);
			intent.putExtra("bsid",bsid);
			context.startActivity(intent);
			SancePopWindow.this.dismiss();
			break;
		case R.id.ll_3:
			intent=new Intent(context,SceneSettingActivity.class);
			intent.putExtra("position",position);
			intent.putExtra("deviceid",deviceid);
			intent.putExtra("sceneid",sceneid);
			intent.putExtra("bsid",bsid);
			context.startActivity(intent);
			SancePopWindow.this.dismiss();
			break;
		case R.id.ll_4:
			deleteScene(bsid,position);
			SancePopWindow.this.dismiss();
			break;

		default:
			break;
		}

	}

	/**
	 * 删除情景
	 */

	public void deleteScene( int bsid,final int position) {
	//	mSVProgressHUD.showErrorWithStatus("正在删除中...");
		JSONObject obj = new JSONObject();
		try{
			obj.put("id",  bsid);
			obj.put("key", "10000");
			obj.put("requestTime", "" + System.currentTimeMillis());
			obj.put("token", (String) SPUtils.get(context, "token", ""));
		} catch (JSONException e1) {
			e1.printStackTrace();
		}
		Log.i("我我我", "response111:" + obj.toString());
		//获取定时数据
		OkHttpUtils.postString().url(HttpURL.SCENE_DELETE)
				.mediaType(MediaType.parse("application/json;charset=UTF-8"))
				.content(obj.toString())
				.build().readTimeOut(5000)
				.writeTimeOut(5000).connTimeOut(5000)
				.execute(new StringCallback() {
					@Override
					public void onError(Request request, Exception e) {
						//showToast("删除失败");
						//mSVProgressHUD.dismiss();
					}
					@Override
					public void onResponse(String response) {

						JSONObject jsonObject = null;
						String value;
						JSONArray jArray;
						try {
							jsonObject = new JSONObject(response);
							value = jsonObject.getString("resultCode");
						//	jArray = (JSONArray) jsonObject.opt("data");
							if (value.equals("1")) {
								Toast.makeText(context,"删除成功",Toast.LENGTH_SHORT).show();
								EventBus.getDefault().post(
										new EventBusEntity("delete_success",position));


							}

						} catch (JSONException e) {
							e.printStackTrace();
						}


					}

				});
	}

}